import pymysql
from pymysql.converters import escape_string


def register():
    print("用户注册")

    user = input("请输入用户名：")
    password = input("请输入密码：")

    # 连接指定数据
    conn = pymysql.connect(host="139.159.152.73", port=3306, user='root', passwd='Python666..', charset='utf8')
    cursor = conn.cursor()

    # 执行SQL语句
    sqll = "CREATE USER %s@'139.159.152.73' IDENTIFIED BY %s;"
    cursor.execute(sqll, (user, password))
    conn.commit()

    # 关闭数据库连接
    cursor.close()
    conn.close()
    print("注册用户成功，用户名：{}，密码：{}".format(user, password))


def login():
    print("用户登录")

    user = input("请输入用户名：")
    password = input("请输入密码：")

    # 连接指定数据
    conn = pymysql.connect(host="139.159.152.73", port=3306, user='root', passwd='Python666..', charset='utf8', db='userdb')
    cursor = conn.cursor()

    conn = pymysql.connect(host="139.159.152.73", port=3306, user='root', passwd='Python666..', charset='utf8')
    cursor = conn.cursor()

    # 关闭连接
    cursor.close()
    conn.close()

    if result:
        print("登录成功", result)
    else:
        print("登陆失败")


def run():
    choice = input("1.注册，2.登录：")
    if choice == '1':
        register()
    elif choice == '2':
        login()
    else:
        print("输入错误")


if __name__ == '__main__':
    run()
